package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.domain.SysBattery;
import com.zhentao.domain.SysBatteryType;
import com.zhentao.domain.SysPowerBattery;
import com.zhentao.domain.SysPowerCabinet;
import com.zhentao.domain.vo.Result;
import com.zhentao.service.SysBatteryService;
import com.zhentao.service.SysBatteryTypeService;
import com.zhentao.service.SysPowerBatteryService;
import com.zhentao.service.SysPowerCabinetService;
import com.zhentao.mapper.SysPowerCabinetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 86133
 * @description 针对表【sys_power_cabinet】的数据库操作Service实现
 * @createDate 2024-08-22 20:40:21
 */
@Service
public class SysPowerCabinetServiceImpl extends ServiceImpl<SysPowerCabinetMapper, SysPowerCabinet>
        implements SysPowerCabinetService {
    @Autowired
    private SysPowerBatteryService sysPowerBatteryService;
    @Autowired
    private SysBatteryService sysBatteryService;
    @Autowired
    private SysBatteryTypeService sysBatteryTypeService;

    @Override
    public Result<List<SysPowerCabinet>> getCabinetInfo(Integer sid) {
        //查询站点下有多少电柜
        List<SysPowerCabinet> cabinetList = this.list(Wrappers.<SysPowerCabinet>lambdaQuery().eq(sid != null, SysPowerCabinet::getSid, sid));
        for (SysPowerCabinet sysPowerCabinet : cabinetList) {
            //查询电柜下有多少电池
            List<SysPowerBattery> batteryList = sysPowerBatteryService.list(Wrappers.<SysPowerBattery>lambdaQuery().eq(SysPowerBattery::getPcid, sysPowerCabinet.getId()));
            //查询电池品牌
            for (SysPowerBattery sysPowerBattery : batteryList) {
                SysBattery one = sysBatteryService.getOne(Wrappers.<SysBattery>lambdaQuery().eq(SysBattery::getId, sysPowerBattery.getBid()));
                SysBatteryType one1 = sysBatteryTypeService.getOne(Wrappers.<SysBatteryType>lambdaQuery().eq(SysBatteryType::getId, one.getBatteryType()));
                if (one1.getBatteryType().equals("60V")) {
                    if (sysPowerCabinet.getMaxBattery() == null) {
                        sysPowerCabinet.setMaxBattery(0);
                    }
                    sysPowerCabinet.setMaxBattery(sysPowerCabinet.getMaxBattery() + sysPowerBattery.getNum());
                }
                if (one1.getBatteryType().equals("40V")) {
                    if (sysPowerCabinet.getMinBattery() == null) {
                        sysPowerCabinet.setMinBattery(0);
                    }
                        sysPowerCabinet.setMinBattery(sysPowerCabinet.getMinBattery() + sysPowerBattery.getNum());

                }
            }
        }
        return Result.build(cabinetList, 200, "操作成功");
    }
}




